import prompt from '@system.prompt';

export default {
    data: {
        todolist: [],
        moveX:0,
        moveStartX:0,
        touchMoveX:0,
        btnAllViewWidth:0,//滑动按钮整体宽
        index:-1,
        isLeft:true,//左侧
        itemBtnArr:[//侧滑按钮定义
            {
                name:'编辑',
                background:'#00aaff',
                color:'#0000',
                width:80,
            },
            {
                name:'删除',
                background:'#ff5500',
                color:'#fffffff',
                width:120,
            },
            {
                name:'收藏',
                background:'#f1215c',
                color:'#868282',
                width:80,
            }
        ],
    },

    onInit() {
        for (var i = 0; i < 4; i++) {
            var items = {
                date : '2021-12-31 20:00:0'+i,
                title : "item ",
                itemMoveX:0,
            }
            this.todolist.push(items);
        }
        for (var i = 0; i < this.itemBtnArr.length; i++) {
            this.btnAllViewWidth += this.itemBtnArr[i].width;
        }

    },
    moveResTo(){
        for (var i = 0; i < this.todolist.length; i++) {
            this.todolist[i].itemMoveX = 0;
        }
    },
    touchstart(index,e){
        this.moveResTo();
        this.moveStartX = e.touches[0].globalX;
//        this.index = msg.target.children[0].watchers[0].vm.$idx;//触摸下标此方法同样有效
        this.index = index;
        console.info("index:"+index+"=="+this.moveStartX);

    },
    touchmove(msg){
        this.touchMoveX = msg.touches[0].globalX;
        console.info("on touchmove is: "+this.touchMoveX);

        if(!this.isLeft){
            if(this.moveStartX>this.touchMoveX){//左滑
                if(this.moveStartX-this.touchMoveX<this.btnAllViewWidth){
                    this.todolist[this.index].itemMoveX = -(this.moveStartX-this.touchMoveX);
                }
            }
        }else{
            if(this.touchMoveX>this.moveStartX){//往右滑
                if(this.touchMoveX-this.moveStartX<this.btnAllViewWidth){
                    this.todolist[this.index].itemMoveX = this.touchMoveX-this.moveStartX
                }
            }
        }
    },

    touchend(){
        if(Math.abs(this.todolist[this.index].itemMoveX)<40){
            this.moveResTo();
        }

        if(Math.abs(this.todolist[this.index].itemMoveX)>=40){

            if(this.isLeft){
                this.todolist[this.index].itemMoveX = this.btnAllViewWidth;
            }else{
                this.todolist[this.index].itemMoveX = -this.btnAllViewWidth;
            }

        }

    },
    itemClick(item){
        prompt.showToast({message:item.name+":"+this.index});
    },


}
